﻿#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dialog)
{
    ui->setupUi(this);
    init();
    initTimer();
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::init()
{
    ui->sb_h_control->setValue(24);
    ui->sl_h_control->setValue(24);
    ui->sl_x_control->setValue(24);
    ui->dial_control->setValue(24);

    //研究一下控件反方向
    ui->sb_x_control->setToolTip("控件提示");
    //设置范围
    ui->sb_x_control->setRange(0,100);
    ui->sb_x_control->setSliderPosition(100);
    ui->sb_x_control->setValue(100-24);

    connect(ui->dial_control, SIGNAL(valueChanged(int)), this, SLOT(do_valueChanged(int)));
    connect(ui->sb_h_control, SIGNAL(valueChanged(int)), this, SLOT(do_valueChanged(int)));
    connect(ui->sl_h_control, SIGNAL(valueChanged(int)), this, SLOT(do_valueChanged(int)));
    connect(ui->sl_x_control, SIGNAL(valueChanged(int)), this, SLOT(do_valueChanged(int)));

    ui->cb_textVisable->setCheckState(Qt::Checked);
    ui->rb_percentage->setChecked(true);
    ui->pb_display->setValue(24);
}


//槽函数处理进度条控件 感觉这个用的比较多
void Dialog::do_valueChanged(int data)
{
    ui->pb_display->setValue(data);
}

void Dialog::on_sb_x_control_valueChanged(int value)
{
    ui->pb_display->setValue(100-value);
}

void Dialog::on_cb_textVisable_clicked(bool checked)
{
    ui->pb_display->setTextVisible(checked);
}

void Dialog::on_cb_inverted_clicked(bool checked)
{
    ui->pb_display->setInvertedAppearance(checked);
}

void Dialog::on_rb_percentage_clicked()
{
    ui->pb_display->setFormat("%p%");
}

void Dialog::on_rb_number_clicked()
{
    ui->pb_display->setFormat("%v");
}

//定时器相关的初始化
#include <QTime >
void Dialog::initTimer()
{
    ui->sb_cycle->setRange(0,20000);
    ui->sb_cycle->setValue(1000);
    ui->sb_cycle->setSuffix(" ms");

    ui->rb_lx_timer->setChecked(true);
    ui->pbn_stop->setEnabled(false);

    lcd_display(true);

    QStringList list ;
    list<<"毫秒级精度"<<"精度的5%"<<"秒精度(粗粒度)";
    ui->cbb_precision->addItems(list);
    ui->cbb_precision->setEditable(false);
    ui->cbb_precision->setCurrentIndex(0);

    m_timer = new QTimer;
    m_timer->stop();
    m_timer->setTimerType(Qt::CoarseTimer); //设置默认精度
    connect(m_timer, SIGNAL(timeout()),this,SLOT(do_timer_timeout()));

    m_cycle_count=0;
}

void Dialog::lcd_display(bool isInit)
{
    if(isInit)
    {
        ui->lcd_hour->display(0);
        ui->lcd_minute->display(0);
        ui->lcd_second->display(0);
        return;
    }
    QTime  cur_timer = QTime::currentTime();
    ui->lcd_hour->display(cur_timer.hour());
    ui->lcd_minute->display(cur_timer.minute());
    ui->lcd_second->display(cur_timer.second());
}

//启动定时器   根据精度选择  单次/重复进行定时器的初始化
void Dialog::on_pbn_start_clicked()
{
    bool is_once_timer = false;
    is_once_timer = ui->rb_once_timer->isChecked()?true:false;

    int time_cycle = ui->sb_cycle->value();
    int time_index = ui->cbb_precision->currentIndex();
    Qt::TimerType time_type = Qt::PreciseTimer;
    switch(time_index)
    {
        case 0: time_type = Qt::PreciseTimer; break;    //比较高的精度
        case 1: time_type = Qt::CoarseTimer; break;     //相对较低的精度
        case 2: time_type = Qt::VeryCoarseTimer; break; //精度比较低
        default: time_type = Qt::PreciseTimer; break;
    }
    lcd_display();

    m_counter.start();
    if(is_once_timer) //创建单次定时器 执行一次
    {
        //创建单次定时器 time_cycle ms之后执行一次  PreciseTimer比较高的精度
//        QTimer::singleShot(time_cycle,Qt::PreciseTimer,this,&Dialog::do_timer_timeout);
        QTimer::singleShot(time_cycle,time_type, [&](){
            int tmMsec = m_counter.elapsed();
            ui->le_timer_display->setText("单次定时器 消耗时间:"+QString::number(tmMsec)+" ms");
            ui->pbn_start->setEnabled(true);
        });

        ui->pbn_start->setEnabled(false);
        //也可以用QTimer 类对象 设置单次属性
        m_timer->setSingleShot(true);
        m_timer->start();
    }

    if(!is_once_timer)//创建定时器 一直执行
    {
        ui->pbn_start->setEnabled(false);
        ui->pbn_stop->setEnabled(true);

        m_timer->setTimerType(time_type);//设置定时器的周期和属性
        m_timer->setInterval(time_cycle);
        m_timer->start();
    }
}

void Dialog::on_pbn_stop_clicked()
{
    m_timer->stop();

    int tmMsec=m_counter.elapsed();  //流逝的时间：毫秒
    int ms= tmMsec % 1000;  //余数毫秒
    int sec=tmMsec/1000;    //整秒

    QString str=QString("流逝的时间:%1秒,%2毫秒").arg(sec).arg(ms,3,10,QChar('0'));
    ui->le_timer_display->setText("执行了"+QString::number(m_cycle_count)+"次,"+str);

    m_cycle_count = 0;
    ui->pbn_start->setEnabled(true);
    ui->pbn_stop->setEnabled(false);
}

//这里是定时器任务的执行 对应lcd显示的触发
void Dialog::do_timer_timeout()
{
    ++m_cycle_count;
    QApplication::beep();   //使系统的蜂鸣器发声 1s

    //获取当前的时间 显示再lcd上
    lcd_display();
}

//获取当前时间 下拉框有日历显示控件 如果要完美，感觉还得自定义控件
void Dialog::on_pbn_getcurtime_clicked()
{
    //设置支持下拉框选择日历 下拉就可以直接选择日期
    ui->dde_cal->setCalendarPopup(true);
    ui->dde_datetime->setCalendarPopup(true);

    //QDate  QTime  QDateTime
    QDateTime cur_time = QDateTime::currentDateTime();
    ui->dde_datetime->setDateTime(cur_time);
    ui->dde_cal->setDate(cur_time.date());
    ui->dde_time->setTime(cur_time.time());

    qint64 MS = cur_time.toSecsSinceEpoch();
//    ui->le_ms_display->setText("当前时间"+QString::number(MS)+"s");
    cur_time.setSecsSinceEpoch(MS+2);//加2s
    ui->le_ms_display->setText("当前时间"+QString::number(MS)+"s"+" 2s后:"+QString::number(cur_time.toSecsSinceEpoch()));

    ui->pte_display->setPlainText("当前日期时间"+cur_time.toString("yyyy-MM-dd hh:mm:ss.zzz"));
    ui->pte_display->appendPlainText("当前日期"+cur_time.date().toString("yyyy-MM-dd"));
    ui->pte_display->appendPlainText("当前时间"+cur_time.time().toString("hh:mm:ss.zzz"));

    //这里需要严格匹配格式
    QString qstr = cur_time.toString("yyyy-MM-dd hh:mm:ss.zzz");
    QString qstr1 = qstr.trimmed();
    QDateTime qstr_date = QDateTime::fromString(qstr1,"yyyy-MM-dd hh:mm:ss.zzz");
    ui->pte_display->appendPlainText("字符串转日期:"+qstr_date.toString("yyyy-MM-dd hh:mm:ss"));
}
//TODO 这里可以增加一个控件选择时间！

void Dialog::on_dde_datetime_dateTimeChanged(const QDateTime &dateTime)
{
    ui->pte_display->setPlainText("选择的日期:"+dateTime.toString("yyyy-MM-dd hh:mm:ss"));
}
